package org.su.generalmvc.jdbc;

import java.util.List;

/**
 * 分页工具类
 * @author leimingbo
 *
 * @param <T>
 */
public class Paging {
	private int currentPage;//当前页数
	private int pageResults;//每页记录数
	private int totalResults;//总记录数
	private int firstResults=0;//起始记录数
	private List<Object> results;//结果集

	public Paging() {
		this(1, 10);
	}

	public Paging(int currentPage,int pageResults) {
		init(currentPage, pageResults);
	}
	
	public Paging(int currentPage,int pageResults,int firstResults) {
		init(currentPage, pageResults);
		setFirstResults(firstResults);
	}
	
	private void init(int currentPage,int pageResults){
		this.currentPage = currentPage;
		if (pageResults <= 0) {
			this.pageResults = 10;
		} else {
			this.pageResults = pageResults;
		}
	}
	
	/**
	 * 初始化Paging对象
	 * @param currentPage 当前页数
	 * @param pageResults 每页记录数
	 * @param totalResults 总记录数
	 * @param results 结果集
	 */
	public Paging(int currentPage,int pageResults,int totalResults,int firstResults,List<Object> results) {
		this.currentPage = currentPage;
		this.pageResults = pageResults;
		this.totalResults = totalResults;
		this.firstResults = firstResults;
		this.results = results;
	}
	
	public void setFirstResults(int firstResult) {
		this.firstResults = firstResult;
	}

	/**
	 * 获得第一条记录的计数
	 * @return
	 */
	public int getFirstResult() {
		int result = ((this.currentPage-1) * this.pageResults) + firstResults;
		
		if (result > this.totalResults){
			result = this.totalResults - 1;
		}
		
		if (result < 0){
			result = 0;
		}
		
		return result;
	}

	/**
	 * 获得当前页数
	 * @return
	 */
	public int getCurrentPage() {
		return this.currentPage;
	}

	/**
	 * 设置当前页数
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * 获得每页记录数
	 * @return
	 */
	public int getPageResults() {
		return this.pageResults;
	}

	/**
	 * 设置每页记录数
	 * @param pageRecords
	 */
	public void setPageResults(int pageRecords) {
		this.pageResults = pageRecords;
	}

	/**
	 * 获得总记录数
	 * @return
	 */
	public int getTotalResults() {
		return this.totalResults;
	}

	/**
	 * 获得总页数
	 * @return
	 */
	public int getTotalPages() {
		return (this.totalResults / this.pageResults + ((this.totalResults
				% this.pageResults > 0) ? 1 : 0));
	}

	/**
	 * 设置总记录数
	 * @param totalResults
	 */
	public void setTotalResults(int totalResults) {
		this.totalResults = totalResults;

		if (this.pageResults <= 0) {
			this.pageResults = totalResults;
		}

		int Pages = getTotalPages();

		if (this.currentPage > Pages)
			this.currentPage = (Pages - 1);

		if (this.currentPage <= 0)
			this.currentPage = 1;
	}
	
	public List<Object> getResults() {
		return results;
	}
	
	public void setResults(List<Object> results) {
		this.results = results;
	}
	
	/**
	 * 结果集添加对象
	 * @param element
	 * @return
	 */
	public boolean addElement(Object element) {
		return this.results.add(element);
	}
	
	/**
	 * 结果集删除对象
	 * @param element
	 * @return
	 */
	public boolean removeElement(Object element) {
		return this.results.remove(element);
	}
}
